package com.itheima.controller;

import com.aliyun.oss.core.AliyunOSSTemplate;
import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
public class UploadController {
   /*
   @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("员工头像文件上传：{}", image);
        *//*
        MultipartFile对象介绍：
        表示上传的临时文件，当方法执行完，临时文件就会删除
        任务：将临时文件存储到指定的地方
        *//*
        //获取原始文件名
        String originalFilename = image.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        //将临时文件移动到指定位置
        image.transferTo(new File("D:\\Temporary Files" + newFileName));
        return Result.ok();
    }
    */

    //修改UploadController代码：
    @Autowired
//    private AliyunOSSUtils aliOSSUtils;
    AliyunOSSTemplate aliyunOSSTemplate;

    @PostMapping("/upload")
    @SneakyThrows
    public Result upload(MultipartFile image)  {
        //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        String url = aliyunOSSTemplate.upload(image);
        //将图片上传完成后的url返回，用于浏览器回显展示
        return Result.ok(url);
    }
}
